<template>
  <a-layout class="ant-pro-layout">
    <a-layout-header class="ant-layout-header">
      <div class="ant-layout-header-left">
        <div class="ant-layout-header-logo">
          <img src="../assets/logo.png" alt="logo" />
          <h1>Data API MCP</h1>
        </div>
      </div>
      <div class="ant-layout-header-right">
        <a-space>
          <a-tooltip :title="isDarkMode ? '切换到亮色模式' : '切换到暗色模式'">
            <a-switch
              v-model:checked="isDarkMode"
              :checkedChildren="'🌙'"
              :unCheckedChildren="'☀️'"
              @change="toggleTheme"
            />
          </a-tooltip>
          <a-dropdown>
            <a-button type="text">
              <template #icon><GlobalOutlined /></template>
              {{ currentLocale === 'zh_CN' ? '中文' : 'English' }}
            </a-button>
            <template #overlay>
              <a-menu @click="handleLocaleChange">
                <a-menu-item key="zh_CN">中文</a-menu-item>
                <a-menu-item key="en_US">English</a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </a-space>
      </div>
    </a-layout-header>

    <a-layout>
      <a-layout-sider
        v-model:collapsed="collapsed"
        class="ant-layout-sider"
        :trigger="null"
        collapsible
        :width="256"
        :collapsedWidth="80"
        :style="{ overflow: 'auto', height: '100vh', position: 'fixed', left: 0 }"
      >
        <div class="ant-layout-sider-menu">
          <a-menu
            v-model:selectedKeys="selectedKeys"
            v-model:openKeys="openKeys"
            theme="dark"
            mode="inline"
            :inline-collapsed="collapsed"
            :style="{ width: collapsed ? '80px' : '256px' }"
          >
            <a-menu-item key="home">
              <template #icon><HomeOutlined /></template>
              <router-link to="/">首页</router-link>
            </a-menu-item>
            <a-menu-item key="database">
              <template #icon><DatabaseOutlined /></template>
              <router-link to="/database">数据库管理</router-link>
            </a-menu-item>
            <a-menu-item key="mcp">
              <template #icon><ApiOutlined /></template>
              <router-link to="/mcp">MCP 服务</router-link>
            </a-menu-item>
            <a-menu-item key="api">
              <template #icon><ApiOutlined /></template>
              <router-link to="/api">API 管理</router-link>
            </a-menu-item>
          </a-menu>
        </div>
        <div class="ant-layout-sider-trigger" @click="() => (collapsed = !collapsed)">
          <menu-unfold-outlined v-if="collapsed" />
          <menu-fold-outlined v-else />
        </div>
      </a-layout-sider>

      <a-layout :style="{ marginLeft: collapsed ? '80px' : '256px', transition: 'all 0.2s' }">
        <a-layout-content class="ant-layout-content">
          <div class="ant-pro-page-header">
            <a-breadcrumb class="ant-breadcrumb">
              <a-breadcrumb-item>
                <router-link to="/" class="ant-breadcrumb-link">
                  <home-outlined class="anticon" />
                  <span>首页</span>
                </router-link>
              </a-breadcrumb-item>
              <template v-if="currentRouteName !== '首页'">
                <a-breadcrumb-item>
                  <span class="ant-breadcrumb-link">{{ currentRouteName }}</span>
                </a-breadcrumb-item>
              </template>
            </a-breadcrumb>
          </div>

          <a-card 
            class="ant-card" 
            :bordered="false" 
            :hoverable="false"
            :bodyStyle="{ padding: '24px' }"
            :headStyle="{ border: 'none' }"
            :style="{ boxShadow: 'none' }"
          >
            <error-boundary>
              <router-view />
            </error-boundary>
          </a-card>
        </a-layout-content>
      </a-layout>
    </a-layout>
    <notification-stack />
  </a-layout>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { useAppStore } from '../stores/app'
import { useRoute } from 'vue-router'
import {
  HomeOutlined,
  DatabaseOutlined,
  ApiOutlined,
  GlobalOutlined,
  MenuUnfoldOutlined,
  MenuFoldOutlined
} from '@ant-design/icons-vue'
import ErrorBoundary from '../components/ErrorBoundary.vue'
import NotificationStack from '../components/NotificationStack.vue'

const appStore = useAppStore()
const route = useRoute()

const collapsed = ref(false)
const selectedKeys = ref(['home'])
const openKeys = ref([])

const currentLocale = computed(() => appStore.locale)
const isDarkMode = computed({
  get: () => appStore.theme === 'dark',
  set: (value) => appStore.setTheme(value ? 'dark' : 'light')
})

const currentRouteName = computed(() => {
  const routeMap = {
    home: '首页',
    database: '数据库管理',
    mcp: 'MCP 服务',
    api: 'API 管理'
  }
  return routeMap[route.path.split('/')[1] || 'home'] || '首页'
})

const toggleTheme = (checked) => {
  appStore.setTheme(checked ? 'dark' : 'light')
  // 更新 body 的 data-theme 属性
  document.body.setAttribute('data-theme', checked ? 'dark' : 'light')
}

const handleLocaleChange = ({ key }) => {
  appStore.setLocale(key)
}

// 根据当前路由更新选中的菜单项
watch(
  () => route.path,
  (path) => {
    const key = path.split('/')[1] || 'home'
    selectedKeys.value = [key]
  },
  { immediate: true }
)

// 初始化主题
onMounted(() => {
  const savedTheme = localStorage.getItem('theme') || 'light'
  appStore.setTheme(savedTheme)
  document.body.setAttribute('data-theme', savedTheme)
})
</script>

<style scoped>
.ant-pro-layout {
  min-height: 100vh;
}

.ant-layout-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  z-index: 1;
  height: 64px;
  line-height: 64px;
  position: fixed;
  width: 100%;
  top: 0;
}

.ant-layout-header-left {
  display: flex;
  align-items: center;
}

.ant-layout-header-logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ant-layout-header-logo img {
  height: 32px;
  margin-right: 8px;
}

.ant-layout-header-logo h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--primary-color);
}

.ant-layout-header-right {
  display: flex;
  align-items: center;
}

.ant-layout-sider {
  background: #001529;
  position: fixed;
  height: 100vh;
  left: 0;
  top: 64px;
  display: flex;
  flex-direction: column;
}

.ant-layout-sider-menu {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.ant-layout-sider-trigger {
  height: 48px;
  line-height: 48px;
  text-align: center;
  color: rgba(255, 255, 255, 0.65);
  background: #002140;
  cursor: pointer;
  transition: all 0.3s;
}

.ant-layout-sider-trigger:hover {
  color: #fff;
  background: #001529;
}

.ant-layout-content {
  margin: 24px;
  margin-top: 64px;
  min-height: calc(100vh - 64px);
}

.ant-pro-page-header {
  margin-bottom: 16px;
  background: #fff;
  padding: 16px 24px;
  border-radius: 2px;
}

.ant-breadcrumb {
  font-size: 14px;
  line-height: 1.5715;
}

.ant-breadcrumb :deep(.ant-breadcrumb-link) {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: rgba(0, 0, 0, 0.45);
}

.ant-breadcrumb :deep(.ant-breadcrumb-separator) {
  margin: 0 8px;
  color: rgba(0, 0, 0, 0.45);
  display: inline-flex;
  align-items: center;
  height: 100%;
}

.ant-breadcrumb :deep(.anticon) {
  font-size: 14px;
  vertical-align: -0.125em;
}

.ant-breadcrumb :deep(a) {
  color: rgba(0, 0, 0, 0.45);
  transition: color 0.3s;
}

.ant-breadcrumb :deep(a:hover) {
  color: var(--primary-color);
}

/* 暗色主题适配 */
[data-theme='dark'] .ant-layout-header {
  background: #1f1f1f;
}

[data-theme='dark'] .ant-pro-page-header {
  background: #1f1f1f;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .ant-layout-header {
    padding: 0 16px;
  }

  .ant-layout-header-logo h1 {
    display: none;
  }

  .ant-layout-content {
    margin: 16px;
    margin-top: 64px;
  }

  .ant-pro-page-header {
    padding: 12px 16px;
  }
}

/* 禁用卡片悬浮效果 */
:deep(.ant-card) {
  transition: none !important;
}

:deep(.ant-card:hover) {
  box-shadow: none !important;
  transform: none !important;
}
</style>